﻿Imports System.Web
Imports Newtonsoft.Json

''' <summary>
''' Class to keep track of Professor information
''' </summary>
''' <remarks></remarks>
<JsonObject(MemberSerialization.OptOut)> _
Public Class Professor
    Public Property name As String
    Public Property website As String
    Public Property email As String
    Public Property notes As String
    Public Property courses As List(Of Course)
    Public Property office As String ' Planned for RC 1.3
    Public Property officeHours As List(Of HourSpan) ' Planned for RC 1.3
    Public Property guid As String

    <JsonIgnore()> _
    Public Shared professorList As List(Of Professor)
    <JsonIgnore()> _
    Public Shared professorEditMenu As MenuItem
    <JsonIgnore()> _
    Public Shared professorDeleteMenu As MenuItem
    <JsonIgnore()> _
    Public itemEdit As New MenuItem
    <JsonIgnore()> _
    Public itemDelete As New MenuItem

    Public Sub New()
        ' set object's GUID. Add checking here at some point?
        guid = String.Concat("_", System.Guid.NewGuid().ToString("N"))
        name = "Professor name here"
        website = ""
        email = ""
        notes = ""
        courses = New List(Of Course)
        office = ""
        officeHours = New List(Of HourSpan)
    End Sub

    Public Sub Edit()
        Dim form As New EditProfessor(Me)
        form.Show()
        itemEdit.Header = name
        itemDelete.Header = name
    End Sub

    Public Sub Create()
        Dim form As New EditProfessor(Me)
        form.ShowDialog()
        If form.Response = MessageBoxResult.OK Then
            professorList.Add(Me)
            addToMenus()
        End If
        form.Close()
    End Sub

    Public Sub addToMenus()
        itemEdit.Header = name
        itemEdit.Name = guid
        AddHandler itemEdit.Click, AddressOf professorMenuRaisedEdit

        itemDelete.Header = name
        itemEdit.Name = guid
        AddHandler itemDelete.Click, AddressOf professorMenuRaisedDelete

        professorEditMenu.Items.Add(itemEdit)
        professorDeleteMenu.Items.Add(itemDelete)
    End Sub


    Public Sub professorMenuRaisedEdit(ByVal sender As MenuItem, ByVal e As System.Windows.RoutedEventArgs)
        Edit()
    End Sub

    Public Sub professorMenuRaisedDelete(ByVal sender As MenuItem, ByVal e As System.Windows.RoutedEventArgs)
        If professorList.Count > 1 Then
            Delete()
        Else
            MessageBox.Show("Sorry! You cannot delete the last professor!" & _
                            vbNewLine & "You must have at least one active professor.")
        End If
    End Sub
    Public Sub Delete()
        professorList.Remove(Me)
        professorEditMenu.Items.Remove(itemEdit)
        professorDeleteMenu.Items.Remove(itemDelete)
    End Sub
End Class
